package dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import dto.AccountDTO;
import help.Utils;

public class AccountDAO extends DatabaseHelper {
	
    public static final String TABLE_ACCOUNT = "Account";
    
    public static final String ACCOUNT_ID = "ID";
    public static final String ACCOUNT_EMAIL = "Email";
    public static final String ACCOUNT_PASSWORD = "Password";
    public static final String ACCOUNT_FIRSTNAME = "FirstName";
    public static final String ACCOUNT_LASTNAME = "LastName";
    public static final String ACCOUNT_ISADMINISTRATOR = "isAdministrator";
    
	public AccountDAO(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
	}

    public int addAccount(AccountDTO accountDTO) 
    {
    	// Open database
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(ACCOUNT_EMAIL, accountDTO.getEmail());
	        values.put(ACCOUNT_PASSWORD, accountDTO.getPassword());
	        values.put(ACCOUNT_FIRSTNAME, accountDTO.getFirstname());
	        values.put(ACCOUNT_LASTNAME, accountDTO.getPassword());
	        values.put(ACCOUNT_ISADMINISTRATOR, accountDTO.getIsAdministrator());
	        
	        // Inserting rows
	        open();
	        long id = DatabaseHelper.myDataBase.insert(TABLE_ACCOUNT, null, values);
	
	        // Close database connection
	        DatabaseHelper.myDataBase.close();
	        return (int)id;
        }
        catch (Exception ex) { return -1;}
    }         
    
    public AccountDTO getAccount(int id) {
        try {
	        Cursor cursor = DatabaseHelper.myDataBase.query(TABLE_ACCOUNT, new String[] { ACCOUNT_ID,
	        		ACCOUNT_EMAIL, ACCOUNT_PASSWORD, ACCOUNT_FIRSTNAME, ACCOUNT_LASTNAME, ACCOUNT_ISADMINISTRATOR }, ACCOUNT_ID + "?",
	            new String[] {String.valueOf(id) }, null, null, null, null);
	        if (cursor != null)
	            cursor.moveToFirst();
	        
	        AccountDTO account = new AccountDTO(
	                Integer.parseInt(cursor.getString(0)),
	                cursor.getString(1),
	                cursor.getString(2),
	                cursor.getString(3),
	                cursor.getString(4),
	                Boolean.parseBoolean(cursor.getString(5)));
	        return account;
        }
        catch (Exception e) {
        	return null;
        }
    }
        
    public List<AccountDTO> getAllAccounts() {
    	try {
	        List<AccountDTO> accountList = new ArrayList<AccountDTO>();
	        
	        // Select all query
	        String selectQuery = "SELECT * FROM " + TABLE_ACCOUNT;
	        open();
	        Cursor cursor = DatabaseHelper.myDataBase.rawQuery(selectQuery, null);
	        
	        // Looping through all rows and adding to list
	        
	        if (cursor.moveToFirst()) {
	            do{
	            	AccountDTO accountDTO = new AccountDTO(
	                		Integer.parseInt(cursor.getString(0)),
	                        cursor.getString(1),
	                        cursor.getString(2),
	                        cursor.getString(3),
	                        cursor.getString(4),
	                        Utils.Int2Boolean(Integer.parseInt(cursor.getString(5))));
	            
	            	accountList.add(accountDTO);
	            } while (cursor.moveToNext());
	        }
	        close();
	        return accountList;
    	}
    	catch (Exception e) {
			return null;
		}
    }
    
    public boolean updateAccount(AccountDTO accountDTO) {
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(ACCOUNT_ID, accountDTO.getID());
	        values.put(ACCOUNT_EMAIL, accountDTO.getEmail());
	        values.put(ACCOUNT_PASSWORD, accountDTO.getPassword());
	        values.put(ACCOUNT_FIRSTNAME, accountDTO.getFirstname());
	        values.put(ACCOUNT_LASTNAME, accountDTO.getLastname());
	        values.put(ACCOUNT_ISADMINISTRATOR, accountDTO.getIsAdministrator());
	        open();
	        // Updating row
	        int isSucceed = DatabaseHelper.myDataBase.update(TABLE_ACCOUNT, values, ACCOUNT_ID + " = ?",
	                new String[] { String.valueOf(accountDTO.getID()) });
	        close();
	        if (isSucceed > 0) {
	        	return true;
	        }
	        	
	        return false;
        }
        catch (Exception ex) { return false; }
    }
    
    public void deleteAccount(AccountDTO accountDTO) {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_ACCOUNT, ACCOUNT_ID + " = ?",
                new String[] { String.valueOf(accountDTO.getID()) });
    		DatabaseHelper.myDataBase.close();
    	}
    	catch (Exception ex) {}
    }
    
    public void deleteAllAccounts() 
    {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_ACCOUNT, null, null);
    	}
    	catch (Exception ex) {}
    }
    
    public int getAccountCount()
    {
    	try {
	        String countQuery = "SELECT * FROM " + TABLE_ACCOUNT;
	        SQLiteDatabase db = this.getReadableDatabase();
	        Cursor cursor = db.rawQuery(countQuery, null);
	        cursor.close();
	        
	        // Return count
	        return cursor.getCount();
    	}
    	catch (Exception ex) {return -1;}
    }
}
